import { Hono } from "hono";
import { z } from "zod";
import validator from "../middlewares/validate";
import { BaselineService } from "../services/baseline";
import Responder from "../middlewares/response";
import { authMiddleware } from "../middlewares/auth";

const baseline = new Hono();
const baselineService = new BaselineService();

const baselineSchema = z.object({
  height: z.number().optional(),
  weight: z.number().optional(),
  birthDate: z.string().optional(),
  bloodType: z.string().optional(),
  allergies: z.array(z.string()).optional(),
  chronicDiseases: z.array(z.string()).optional(),
  medications: z.array(z.string()).optional(),
});

// 获取基线数据
baseline.get("/", authMiddleware, async (c) => {
  const userId = c.get("userId");
  const result = await baselineService.getBaseline(userId);
  return Responder.success()
    .setData(result || {})
    .build(c);
});

// 创建或更新基线数据
baseline.post(
  "/",
  authMiddleware,
  validator("json", baselineSchema),
  async (c) => {
    const userId = c.get("userId");
    const data = c.req.valid("json");

    const result = await baselineService.createOrUpdateBaseline(userId, {
      ...data,
      birthDate: data.birthDate ? new Date(data.birthDate) : undefined,
    });

    return Responder.success("基线数据更新成功").setData(result).build(c);
  }
);

// 删除基线数据
baseline.delete("/", authMiddleware, async (c) => {
  const userId = c.get("userId");
  await baselineService.deleteBaseline(userId);
  return Responder.success("基线数据删除成功").build(c);
});

export default baseline;
